草庐IT

带指针的 C++ vector

全部标签

c++ - std::vector 手动中毒

在下面的代码片段中,有一个错误不是微不足道的,但我希望像AddressSanitizer这样的工具能够捕捉到它。#include#includeintmain(){std::vectortoto;toto.push_back(2);intconst&titi=toto[0];toto.pop_back();std::cout当在vector范围内打印并在范围外打印catch引用时,会抛出use-heap-after-free错误。但是当没有作用域时,std::vector实现可能不会在pop_back之后释放内存,因此引用仍然指向有效内存。我四处搜索,发现您可以手动毒化内存,我想知道这

c++ - 我的 g++ 使用 vector<weak_ptr> erase() 方法生成奇怪的警告

我有以下C++代码:#include#include#include#includevoiderase_from_vector(std::vector>&mvec){for(automvec_it=mvec.begin();mvec_it!=mvec.end();)mvec_it=mvec.erase(mvec_it);}intmain(void){#if0std::vector>mvec;for(automvec_it=mvec.begin();mvec_it!=mvec.end();)mvec_it=mvec.erase(mvec_it);#endif}当我这样编译时,GCC会生成

c++ - 函数指针引用参数转换为const

示例代码:classFoo;typedefvoid(*fnptr)(Foo&foo);fnptrgFn;voidmyfoo(constFoo&foo){}intmain(){gFn=&myfoo;}使用clang失败并出现以下错误:main.cpp:9:9:error:assigningto'fnptr'(aka'void(*)(Foo&)')fromincompatibletype'void(*)(constFoo&)':typemismatchat1stparameter('Foo&'vs'constFoo&')gFn=&myfoo;^~~~~~~1errorgenerated.G

c++ - 继承类中的 shared_from_this() 类型错误(是否有 dyn.type-aware 共享指针?)

我有一个使用enable_shared_from_this的ViewController基类classViewController:publicstd::enable_shared_from_this{//...};和一个child:classGalleryViewController:publicViewController{voidupdateGallery(floatdelta);}问题出现了,当我尝试将我当前的实例传递给第3方时(比如lambda函数被安排在某处)实例(GalleryViewController)会释放一个(罕见的)条件,所以我不能直接捕获“this”,我需要用

c++ - 在 void 指针中存储整数的往返安全性

在thisarticle关于整数和指针的reinterpret_cast提到了以下内容:(theround-tripconversionintheoppositedirectionisnotguaranteed;thesamepointermayhavemultipleintegerrepresentations)我的理解是否正确,标准不保证以下内容:intptr_tx=5;void*y=reinterpret_cast(x);assert(x==reinterpret_cast(y));有人可以确认吗? 最佳答案 您的解释是正确的

【C++】vector模拟实现+迭代器失效

vector模拟实现成员变量定义默认成员函数构造函数迭代器范围for、对象类型匹配原则容量操作sizeemptycapacityreserve成员变量未更新memcpy值拷贝resize内置类型的构造函数数据访问frontbackoperator[]数据修改操作push_backpop_backswapclearinsertpos位置未更新无返回值erase无返回值迭代器失效定义insert导致的迭代器失效erase导致的迭代器失效删除vector中的奇数非法的间接寻址铁汁们,今天给大家分享一篇vector模拟实现+迭代器失效,来吧,开造⛳️成员变量定义指向最后一个空间的下一个位置💡itera

c++ - 如何告诉 C++ 根据索引指示符放弃 vector 中的某些元素

我正在使用RCPP来加速我项目中的R代码。现在我正在做的是使用Armadillo包将我的R代码转换为C++。我发现我经常用C++编写多行来替换R中的一行...这是我的问题:我有一个vector存储数据:数据。我还有一个矩阵存储我需要访问的元素的索引。请允许我先用R说明我的场景:>Data[1]45678和>index[,1][,2][,3][1,]123[2,]100[3,]202对于“索引”矩阵的每一行,我想从数据中获取相应的元素。在R中,我只需要这样编码:>Data[index[1,]][1]456>Data[index[2,]][1]4>Data[index[3,]][1]55即

c++ - 给定迭代器列表,如何从 vector 中删除元素?

我有一个intvector和一个映射,其中包含一些指向该vector的迭代器作为值。我需要从map中删除键,以及值指向的vector元素。我的代码看起来像这样:usingRenderData=int;usingElement=std::string;structRef{std::vector::iteratorref;std::functionupdate;boolshould_remove;};intmain(){std::vectorints{1,2,3,4,5,6,7,8,9};std::unordered_mapelements;//Here,Ineedtoremovesome

c++ - 指针和 std::string - 奇怪的行为 - C++

我提前道歉,因为我在之前的帖子中问过同样的问题,但正如有人正确指出的那样,我没有发布真正的代码。因此,我再次问同样的问题,试图比以前更清楚。作为练习,我正在创建一个操作字符串的程序。特别是,我想删除包含在2个“*”之间的部分字符串。我必须强调,我已经使用库字符串的函数成功地创建了相同的程序;事实上,问题涉及使用char指针对给定字符串的操作。我将发布完整的代码并进行深入讨论。#include#includeusingnamespacestd;intmain(){stringfrase;getline(cin,frase);//Takesasinputthephraseintsize=f

c++ - Xcode C++ vector : Implicit instantiation of undefined template

我在不同的IDE上运行这段代码,它成功了。出于某种原因,我在Xcode上收到上述错误消息。我假设我缺少某种标题,但我不确定是哪一个。#include#include#include#includeintmain(){vectorlistRestaurants;//error:Implicitinstantiationofundefinedtemplatereturn0;} 最佳答案 Xcode10.2.1向我展示了错误Implicitinstantiationofundefinedtemplate'std::__1::vector,